<template>
  <div class="account-security">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="注册" name="register">
        <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-width="100px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="registerForm.password" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" type="password" show-password></el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="修改密码" name="password">
        <el-form :model="passwordForm" :rules="passwordRules" ref="passwordForm" label-width="100px">
          <el-form-item label="当前密码" prop="currentPassword">
            <el-input v-model="passwordForm.currentPassword" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="passwordForm.newPassword" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="confirmPassword">
            <el-input v-model="passwordForm.confirmPassword" type="password" show-password></el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="账户安全" name="security">
        <el-form :model="securityForm" label-width="100px">
          <el-form-item label="手机绑定">
            <el-input v-model="securityForm.phone" disabled>
              <template slot="append">
                <el-button type="text" @click="handlePhoneChange">修改</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="邮箱绑定">
            <el-input v-model="securityForm.email" disabled>
              <template slot="append">
                <el-button type="text" @click="handleEmailChange">修改</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="登录设备">
            <el-button type="text" @click="handleDeviceManage">管理登录设备</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <div class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AccountSecurity',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.passwordForm.confirmPassword !== '') {
          this.$refs.passwordForm.validateField('confirmPassword');
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    const validateRegisterPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.confirmPassword !== '') {
          this.$refs.registerForm.validateField('confirmPassword');
        }
        callback();
      }
    };
    const validateRegisterPass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      activeTab: 'register',
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validateRegisterPass, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateRegisterPass2, trigger: 'blur' }
        ]
      },
      passwordForm: {
        currentPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      passwordRules: {
        currentPassword: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认新密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      securityForm: {
        phone: '138****8888',
        email: 'user@example.com'
      }
    }
  },
  methods: {
    handlePhoneChange() {
      this.$message.info('修改手机号功能开发中');
    },
    handleEmailChange() {
      this.$message.info('修改邮箱功能开发中');
    },
    handleDeviceManage() {
      this.$message.info('设备管理功能开发中');
    },
    handleCancel() {
      this.$emit('cancel');
    },
    handleSubmit() {
      if (this.activeTab === 'register') {
        this.$refs.registerForm.validate((valid) => {
          if (valid) {
            this.$axios.post('/api/register', {
              username: this.registerForm.username,
              password: this.registerForm.password
            }).then(res => {
              if (res.data.status === 200) {
                this.$message.success('注册成功');
                this.$emit('cancel');
              } else {
                this.$message.error(res.data.errors || '注册失败');
              }
            }).catch(err => {
              this.$message.error(`注册失败：${err.message || '请稍后重试'}`);
            });
          }
        });
      } else if (this.activeTab === 'password') {
        this.$refs.passwordForm.validate((valid) => {
          if (valid) {
            // 这里添加修改密码的API调用
            this.$message.success('密码修改成功');
            this.$emit('cancel');
          }
        });
      } else {
        // 这里添加账户安全设置的保存逻辑
        this.$message.success('设置保存成功');
        this.$emit('cancel');
      }
    }
  }
}
</script>

<style scoped>
.account-security {
  padding: 20px;
}

.dialog-footer {
  margin-top: 20px;
  text-align: right;
}

.el-form {
  max-width: 500px;
  margin: 20px auto;
}
</style> 